package com.shinhye.mobilelottery.bean;

import java.util.ArrayList;
import java.util.List;

import com.shinhye.mobilelottery.GlobalParams;

public class ShoppingCart {
	private ShoppingCart(){
		ticketBag = new ArrayList<Ticket>();
	}
	private static ShoppingCart instance = new ShoppingCart();
	public static ShoppingCart getInstance() {
		return instance;
	}
	
//	lotteryid string * 玩法编号
//	issue string * 期号（当前销售期）
//	lotterycode string * 投注号码，注与注之间^分割
//	lotterynumber string  注数
//	lotteryvalue string  方案金额，以分为单位
//	appnumbers string  倍数
//	issuesnumbers string  追期
//	issueflag int * 是否多期追号 0否，1多期
//	bonusstop int * 中奖后是否停止：0不停
	private int lotteryid;
	private String issue;
	private List<Ticket> ticketBag;
	private int lotterynumber;
	private int lotteryvalue;
	private int appnumbers = 1;
	private int issuesnumbers = 1;
	
	/**
	 * Add or reduce the appnumbers.
	 * @param toAdd If true to add, otherwise reduce.
	 */
	public boolean changeAppnumbers(boolean toAdd) {
		if(toAdd) {
			appnumbers++;
			if(getLotteryvalue() > GlobalParams.money || appnumbers > 99) {
				appnumbers--;
				return false;
			}
		} else {
			appnumbers--;
			if(appnumbers < 1) {
				appnumbers++;
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Add or reduce the issuesnumbers.
	 * @param toAdd If true to add, otherwise reduce.
	 */
	public boolean changeIssuesnumbers(boolean toAdd) {
		if(toAdd) {
			issuesnumbers++;
			if(getLotteryvalue() > GlobalParams.money || issuesnumbers > 99) {
				issuesnumbers--;
				return false;
			}
		} else {
			issuesnumbers--;
			if(issuesnumbers < 1) {
				issuesnumbers++;
				return false;
			}
		}
		return true;
	}
	
	public void setLotteryid(int lotteryid) {
		this.lotteryid = lotteryid;
	}

	public void setIssue(String issue) {
		this.issue = issue;
	}

	public int getLotteryid() {
		return lotteryid;
	}

	public String getIssue() {
		return issue;
	}

	public int getLotterynumber() {
		lotterynumber = 0;
		for(Ticket ticket : ticketBag) {
			lotterynumber += ticket.getRecord();
		}
		return lotterynumber;
	}

	public int getLotteryvalue() {
		lotteryvalue = lotterynumber * 2 * appnumbers * issuesnumbers;
		return lotteryvalue;
	}

	public int getAppnumbers() {
		return appnumbers;
	}

	public int getIssuesnumbers() {
		return issuesnumbers;
	}

	public List<Ticket> getTicketBag() {
		return ticketBag;
	}
	
	/**
	 * Clear and resume the shopping cart information. 
	 */
	public void clear() {
		ticketBag.clear();
		appnumbers=1;
		issuesnumbers=1;
		
	}

}
